package spring.cloud.eurekaclientproduct.cmd;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.apache.http.client.methods.HttpHead;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import com.spring.cloud.commonutils.vo.UserInfo;
import com.spring.cloud.commonutils.vo.ResultMessage;

public class UserPutCommand extends HystrixCommand<UserInfo> {

    private RestTemplate restTemplate;
    private UserInfo userInfo = null;
    private String URL = "http://eureka-client-user/user/info";

    public UserPutCommand(RestTemplate restTemplate,Long id, String username,String note) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userGroup")));
        this.restTemplate = restTemplate;
        userInfo = new UserInfo(id,username,note);
    }

    @Override
    protected UserInfo run() throws Exception {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(MediaType.APPLICATION_JSON_VALUE));
        HttpEntity<UserInfo> request = new HttpEntity<>(userInfo,headers);
        System.out.println("执行更新用户"+userInfo.getId());
        restTemplate.put(URL,request);
        UserGetCommand.clearCache(userInfo.getId());
        return userInfo;
    }
}
